import os
from PIL import Image

def resize_and_convert_to_1bit(input_image_path, output_image_path, size=(1024, 512), threshold=128):
    with Image.open(input_image_path) as img:
        img_resized = img.resize(size, Image.Resampling.LANCZOS)
        img_gray = img_resized.convert('L')
        img_1bit = img_gray.point(lambda x: 0 if x < threshold else 1, '1')
        img_1bit.save(output_image_path)
        print(f"图片已调整为 {size[0]}x{size[1]} 并转换为1bit灰度图，保存到 {output_image_path}")

def resize_image(input_image_path, output_image_path, size=(1024, 512)):
    with Image.open(input_image_path) as img:
        img_resized = img.resize(size, Image.Resampling.LANCZOS)
        img_resized.save(output_image_path)
        print(f"图片已调整为 {size[0]}x{size[1]}，保存到 {output_image_path}")

def process_directory(input_dir, output_dir_1bit, output_dir_resized, size=(1024, 512), threshold=128):
    os.makedirs(output_dir_1bit, exist_ok=True)
    os.makedirs(output_dir_resized, exist_ok=True)

    for image_file in os.listdir(input_dir):
        input_image_path = os.path.join(input_dir, image_file)
        if not os.path.isfile(input_image_path):
            continue

        output_image_path_1bit = os.path.join(output_dir_1bit, image_file)
        output_image_path_resized = os.path.join(output_dir_resized, image_file)

        # resize_and_convert_to_1bit(input_image_path, output_image_path_1bit, size, threshold)
        resize_image(input_image_path, output_image_path_resized, size)

input_dir = '/home/jacy/PyPjs/data/coco_val2017/'  # 替换为你的输入文件夹路径
output_dir_1bit = '/home/jacy/PyPjs/detection/eval_inf/dif_pic/1byte_dir'  # 替换为你想要保存1bit图像的文件夹路径
output_dir_resized = '/home/jacy/PyPjs/detection/eval_inf/dif_pic/3byte_dir/'  # 替换为你想要保存调整大小图像的文件夹路径

process_directory(input_dir, output_dir_1bit, output_dir_resized)